//
//  EditTwitterEmail.m
//  Bespoke
//
//  Created by Cheng on 12/10/11.
//
//

#import "EditTwitterEmail.h"
#import "AccountInfo.h"
#import "AccountParser.h"
#import "RequestAPIFunction.h"

#define USERINFO_API @"http://54.235.166.43/BeSpokeAPI/userInfo.php"

@implementation EditTwitterEmail
@synthesize Tf_Email, NS_FullName, Bt_CONTINUE, Lab_UserName;
@synthesize Array_AccountInfo, BK_En_object;
@synthesize delegate;

- (IBAction)CancelPressed:(id)sender {
    
    [self.delegate CancelPressed_EditTwitterEmail];
}

- (IBAction)CONTINUEPressed:(id)sender {
    
    NSString *NS_Email = [NSString stringWithFormat:@"%@%@",Lab_UserName.text,Tf_Email.text];
    
    NSData *responseData = 
    [RequestAPIFunction XML_PostRequest:[NSString stringWithFormat:@"<Authentication UserName=\"%@\" Password=\"\" Name=\"%@\" Source =\"Twitter\" Plaintext=\"True\" />",
                               NS_Email, NS_FullName]
                    urlString:USERINFO_API];
    
    //No Network Connection or time out.
    if (responseData == nil)
        return;
    
    Array_AccountInfo = [AccountParser loadAccountInfo:responseData];
    
    if (([Array_AccountInfo count]!=0)&&(![Tf_Email.text isEqualToString:@""])) {
        
        AccountInfo *AccountInfoOB = (AccountInfo *)[Array_AccountInfo objectAtIndex:0];
        
        [self.BK_En_object setAuthenticationUserName:NS_Email Password:nil];
        [self.BK_En_object setAccountInfoFullName:AccountInfoOB.NS_AccountFullName setPartner:nil];
        
        [self.BK_En_object setAccountInfoFaceBookID:AccountInfoOB.Feed.Ob_Facebook.NS_FacebookID setFaceBookTotken:AccountInfoOB.Feed.Ob_Facebook.NS_FacebookToken];
        [self.BK_En_object setFacebookONorOff:AccountInfoOB.Feed.Ob_Facebook.FacebookSwitch];
        
        [self.BK_En_object setAccountInfoTwitterKey:AccountInfoOB.Feed.Ob_Twitter.NS_TwitterKey setTwitterSecret:AccountInfoOB.Feed.Ob_Twitter.NS_TwitterSecret];
        [self.BK_En_object setTwitterONorOff:AccountInfoOB.Feed.Ob_Twitter.TwitterSwitch];
        
        [self.BK_En_object setAccountInfoTumblr:AccountInfoOB.Feed.Ob_Tumblr.NS_TumblrName];
        [self.BK_En_object setTumblrONorOff:AccountInfoOB.Feed.Ob_Tumblr.TumblrSwitch];
        
        [self.BK_En_object setAccountInfoInstagram:AccountInfoOB.Feed.Ob_Instagram.NS_InstagramToken];
        [self.BK_En_object setInstagramONorOff:AccountInfoOB.Feed.Ob_Instagram.InstagramSwitch];
        
        for (AccountGoogleFeed *CurrentGoogleFeed in AccountInfoOB.Feed.Array_GoogleFeed) {
            
            int GoogleIndex = [AccountInfoOB.Feed.Array_GoogleFeed indexOfObject:CurrentGoogleFeed];
            
            [self.BK_En_object setAccountInfoGoogleReader:CurrentGoogleFeed.NS_GoogleTitle
                                               SetContent:CurrentGoogleFeed.NS_GoogleContent
                                               SetWebLink:CurrentGoogleFeed.NS_GoogleLink];
            [self.BK_En_object setGoogleSwitchCheck:CurrentGoogleFeed.GoogleFeedSwitch
                                           SetIndex:GoogleIndex];
        }
        
        
        UIAlertView *AlertView = [[UIAlertView alloc]initWithTitle:@"Success" message:@"Login has been sucessful." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [AlertView setTag:101];
        [AlertView show];
    }
    else {
        UIAlertView *AlertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"The username or password you entered is incorrect." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [AlertView show];
    }
}

#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    
    //Login Success alert.
    if (alertView.tag == 101) {
        [self.delegate SignInSuccess_UseTwitter];
    }
    else if (alertView.tag == 102) {
    }
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    if(textField == Tf_Email)
        [self CONTINUEPressed:textField];
    return YES;
}

@end
